package stream;

import java.util.Optional;
import java.util.function.Consumer;

public class OptionalTest {
	
	public static void main(String[] args) {
		Optional<Integer> optional1 = Optional.ofNullable(2);
		Optional<Integer> optional2 = Optional.ofNullable(null);
		//isPresnet判断值是否存在
		System.out.println(optional1.isPresent());
		System.out.println(optional2.isPresent());
		
		//如果不是null,则调用
		optional1.ifPresent(new Consumer<Integer>() {

			@Override
			public void accept(Integer t) {
				System.out.println(t);
			}
		});
		optional2.ifPresent(new Consumer<Integer>() {
			
			@Override
			public void accept(Integer t) {
				System.out.println(t);
			}
		});
		
		Optional<Optional<String>> strOptional1 = optional1.map(a -> {
			return Optional.<String>of("key-"+a);
		});
		System.out.println(strOptional1.get().get());
			
		Optional<String> strOptional2 = optional1.flatMap(a -> {
			return Optional.of("key-"+a);
		});
		System.out.println(strOptional2.get());
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	

}
